rm(list = ls())
gc()

library(data.table)
library(dplyr)
library(stringr)
library(tidyr)
library(gender)
library(lfe)

## SURVEY PARTISAN SEGREGATION ANALYSIS WITH WEIGHTS



surv=fread('survey-analysis.csv.gz')


surv[countyfips=='',countyfips:=NA]
surv[ZipCode=='',ZipCode:=NA]

surv[,Race:=factor(Race,levels=c('Other','Asian','Black','Hispanic','White'))]


# Perceptions of neighborhoods


m = felm(NeighborDems~ DemSpExp_nohh +hh.d.adj+hh.n.adj  + WhiteBlockGroup + AgeBlockGroup + RegsBlockGroup + HHIncomeBlockGroup  + CollegeBlockGroup + HomeownerBlockGroup + YearBuiltBlockGroup +DriveWorkBlockGroup + EmplBlockGroup + HouseValueBlockGroup + Race + Age + Male + Liberalism + Married  + College + Homeowner + YearsResidence+Party7 |ZipCode|0|countyfips,data=surv, weights =  surv$w)
w1DemSpExp = summary(m,robust=T)
w1DemSpExp$mean.outcome=weighted.mean(m$response, w=m$weights)


m = felm(NeighborDems~ RepSpExp_nohh  +hh.r.adj+hh.n.adj + WhiteBlockGroup + AgeBlockGroup + RegsBlockGroup + HHIncomeBlockGroup  + CollegeBlockGroup + HomeownerBlockGroup + YearBuiltBlockGroup +DriveWorkBlockGroup + EmplBlockGroup + HouseValueBlockGroup + Race + Age + Male + Liberalism + Married  + College + Homeowner + YearsResidence+Party7 |ZipCode|0|countyfips,data=surv, weights =  surv$w)
w1RepSpExp = summary(m,robust=T)
w1RepSpExp$mean.outcome=weighted.mean(m$response, w=m$weights)


#### Contact with neighbor dems

m = felm(ContactDemNeighbors~ DemSpExp_nohh +hh.d.adj+hh.n.adj  + WhiteBlockGroup + AgeBlockGroup + RegsBlockGroup + HHIncomeBlockGroup  + CollegeBlockGroup + HomeownerBlockGroup + YearBuiltBlockGroup +DriveWorkBlockGroup + EmplBlockGroup + HouseValueBlockGroup + Race + Age + Male + Liberalism + Married  + College + Homeowner + YearsResidence+Party7 |ZipCode|0|countyfips,data=surv, weights =  surv$w)
w2DemSpExp = summary(m,robust=T)
w2DemSpExp$mean.outcome=weighted.mean(m$response, w=m$weights)

m = felm(ContactRepNeighbors~ RepSpExp_nohh +hh.r.adj+hh.n.adj + WhiteBlockGroup + AgeBlockGroup + RegsBlockGroup + HHIncomeBlockGroup  + CollegeBlockGroup + HomeownerBlockGroup + YearBuiltBlockGroup +DriveWorkBlockGroup + EmplBlockGroup + HouseValueBlockGroup + Race + Age + Male + Liberalism + Married  + College + Homeowner + YearsResidence+Party7 |ZipCode|0|countyfips,data=surv, weights =  surv$w)
w2RepSpExp = summary(m,robust=T)
w2RepSpExp$mean.outcome=weighted.mean(m$response, w=m$weights)




# Comfort 

m = felm(Comfort~(DemSpExp_nohh + hh.d.adj+hh.n.adj+ WhiteBlockGroup + AgeBlockGroup + RegsBlockGroup + HHIncomeBlockGroup  + CollegeBlockGroup + HomeownerBlockGroup + YearBuiltBlockGroup +DriveWorkBlockGroup + EmplBlockGroup + HouseValueBlockGroup + Race + Age + Male + Liberalism + Married  + College + Homeowner + YearsResidence+Party7)*Democrat |ZipCode|0|countyfips,data=surv, weights =  surv$w)
w5DemSpExp = summary(m,robust=T)
w5DemSpExp$mean.outcome=weighted.mean(m$response, w=m$weights)

m = felm(Comfort~(RepSpExp_nohh  + hh.r.adj+hh.n.adj+ WhiteBlockGroup + AgeBlockGroup + RegsBlockGroup + HHIncomeBlockGroup  + CollegeBlockGroup + HomeownerBlockGroup + YearBuiltBlockGroup +DriveWorkBlockGroup + EmplBlockGroup + HouseValueBlockGroup + Race + Age + Male + Liberalism + Married  + College + Homeowner + YearsResidence+Party7)*Republican |ZipCode|0|countyfips,data=surv, weights =  surv$w)
w5RepSpExp = summary(m,robust=T)
w5RepSpExp$mean.outcome=weighted.mean(m$response, w=m$weights)




# Predicting party switching as function of comfort models
dems = surv[Party_year1=='Democrat']
reps = surv[Party_year1=='Republican']
oths = surv[Party_year1=='Other']




m = felm(Democrat~ Comfort + Race + Age + Male  + Married  + College + Homeowner + YearsResidence |ZipCode|0|0,data=dems, weights =  dems$w)
w6DemSpExpDems = summary(m,robust=T)
w6DemSpExpDems$mean.outcome=weighted.mean(m$response, w=m$weights)


m = felm(Democrat~ Comfort + Race + Age + Male  + Married  + College + Homeowner + YearsResidence |ZipCode|0|0,data=reps, weights =  reps$w)
w6DemSpExpReps = summary(m,robust=T)
w6DemSpExpReps$mean.outcome=weighted.mean(m$response, w=m$weights)

m = felm(Democrat~ Comfort + Race + Age + Male  + Married  + College + Homeowner + YearsResidence |ZipCode|0|0,data=oths, weights =  oths$w)
w6DemSpExpOths = summary(m,robust=T)
w6DemSpExpOths$mean.outcome=weighted.mean(m$response, w=m$weights)



m = felm(Republican~ Comfort + Race + Age + Male  + Married  + College + Homeowner + YearsResidence |ZipCode|0|0,data=dems, weights =  dems$w)
w6RepSpExpDems = summary(m,robust=T)
w6RepSpExpDems$mean.outcome=weighted.mean(m$response, w=m$weights)


m = felm(Republican~ Comfort + Race + Age + Male  + Married  + College + Homeowner + YearsResidence |ZipCode|0|0,data=reps, weights =  reps$w)
w6RepSpExpReps = summary(m,robust=T)
w6RepSpExpReps$mean.outcome=weighted.mean(m$response, w=m$weights)

m = felm(Republican~ Comfort + Race + Age + Male  + Married  + College + Homeowner + YearsResidence |ZipCode|0|0,data=oths, weights =  oths$w)
w6RepSpExpOths = summary(m,robust=T)
w6RepSpExpOths$mean.outcome=weighted.mean(m$response, w=m$weights)




##
rm(surv,w,data,dems,reps,oths,m,t)
save.image(file='results/survey-results-weights.Rdata')